package net.viralpatel.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class ListRoomAction extends ActionSupport{
	private List<Room> listRoom = new ArrayList<Room>();
	String url = "jdbc:mysql://localhost:3306/";
	String dbName = "t10";
	String driverName = "com.mysql.jdbc.Driver";
	Connection con = null;
	ResultSet resultSet = null;
	String nameroot = "root";
	String passwordroot = "";
	
	public String execute() throws Exception {
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url + dbName, nameroot,
					passwordroot);
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		String selectListRoom = "SELECT roomID, roomType2, seat, floor, price, status,description FROM room ORDER BY roomID";
		PreparedStatement prepStmt = con.prepareStatement(selectListRoom);
		resultSet = prepStmt.executeQuery();
		if (resultSet.next()) {
			resultSet.beforeFirst();
			while (resultSet.next()) { // process results one row at a time
				Room room = new Room();
				room.setRoomID(resultSet.getString(1).toString());
				room.setRoomType2(resultSet.getString(2).toString());
				room.setSeat(resultSet.getInt(3));
				room.setFloor(resultSet.getInt(4));
				room.setPrice(resultSet.getInt(5));
				room.setStatus(resultSet.getInt(6));
				room.setDesc(resultSet.getString(7).toString());
				listRoom.add(room);
			}
			con.close();
			return SUCCESS;
		}else{
			addActionError(getText("error.listRoom"));
			con.close();
			return ERROR;
		}	
	}
	public void setListRoom(List<Room> listRoom) {
		this.listRoom = listRoom;
	}

	public List<Room> getListRoom() {
		return listRoom;
	}

}
